<?
if( !$PARAPARA || !$PARAPARA->is_admin() )
    return;

if($do == 'form_action') {
    $form_action = '';
    if($_POST['del'] )
        $form_action = 'del';
    elseif($_POST['del_all'])
        $form_action = 'del_all';

    $dbl = $PARAPARA->dblayer;

    if($form_action == 'del') {
        $marked_msgs = array();
        foreach($_POST as $key => $value) {
            if( preg_match('/^msg_(\d+)$/', $key, $matches ) ) {
                array_push( $marked_msgs, $matches[1]);
            }
        }

        if(sizeof($marked_msgs)) {
            $dbl->del_chat_messages( $marked_msgs);
            if(sizeof($marked_msgs) == 1 )
                $info_text = "Deleted message";
            else
                $info_text = "Deleted " . sizeof( $marked_msgs ) ." messages";
        
            echo '<div class="padm_info">'; 
            echo $info_text;
            echo "</div>\n";

        }
        else {
            echo '<div class="padm_info">'; 
            echo 'No messages marked';
            echo "</div>\n";
        }
    }

    elseif($form_action == 'del_all') {
        $dbl->del_all_chat_messages();

        echo '<div class="padm_info">'; 
        echo 'Deleted all messages';
        echo "</div>\n";

    }

}

elseif( $do == 'enable' ) {

    $PARAPARA->config_set_save( 'CHAT', 'ON', 'true' );

    echo '<div class="padm_info">'; 
    echo 'Enabled chat. The chat is now open for shouts.';
    echo "</div>\n";
}

elseif( $do == 'disable') {

    $PARAPARA->config_set_save( 'CHAT', 'ON', 'false' );

    echo '<div class="padm_info">'; 
    echo 'Disabled chat. The chat is now closed.';
    echo "</div>\n";
}


// functions ////////////////////////////////////////

function list_chat() {
    global $PARAPARA;

    $dbl      = $PARAPARA->dblayer;
    $shouts = $dbl->get_chat_messages( 0 );
    $chat     = new Chat( $shouts );

    $buttons = 
        '<div class="padm_chat_msglist_buttons">' .
        '<input type="submit" class="button" name="del" value="Delete" />' .
        '<input type="submit" class="button" name="del_all" value="Delete All" />' .
        "</div>\n";

    if( sizeof($shouts) ) {
        echo '<form action="?sec=chat&do=form_action" method="POST">';
        echo '<table class="padm_chat_msglist">' . "\n";
        echo $buttons;
        foreach ($shouts as $ss) {
            $sid = $ss->id;
            $checkbox = "<input name=\"msg_$sid\" value=\"$sid\" type=\"checkbox\" />";
            $screen_name = 
                $ss->uid ?
                "<a href=\"?sec=acc&ssec=edit&uid=$ss->uid\">" . 
                htmlspecialchars( $ss->screen_name ) .'</a>' :
                htmlspecialchars( $ss->screen_name ) ;

            echo '<tr class="padm_chat_msg">';
            echo '<td class="padm_chat_msg_id">'.   $ss->id . '</td>';
            echo '<td class="padm_chat_msg_checkbox">' . $checkbox . '</td>';
            echo '<td class="padm_chat_msg_sn">'. $screen_name   . '</td>';
            echo '<td class="padm_chat_msg_text">'. htmlspecialchars( $ss->text ) . '</td>';
            echo '<td class="padm_chat_msg_time">'. $ss->time . '</td>';
            echo '</tr>';
        }
        echo "</table>\n";
        echo $buttons;
        echo "</form>\n";
    }
    else {
        echo '<div class="padm_info">No messages in the chat</div>';
    }
}


function togglebox() {
    global $PARAPARA;

    $state = $PARAPARA->config_get('CHAT', 'ON');

    echo "<div class='padm_chat' />";
    if($state == 'true') {
        echo 'The chat is enabled. <a href="?sec=chat&do=disable">Disable chat</a>';
    }
    else {
        echo 'The chat is disabled. <a href="?sec=chat&do=enable">Enable chat</a>';
    }
    echo "</div>\n";
}

?>

<h2>Chat Administration</h2>

<div class="padm_chat">
<?
togglebox();
list_chat();
 ?>
</div>
